<?php

namespace App\Models\Store\Ranch;

use App\Helpers\MvcHelper;
use App\Models\Store\User\UserModel;

class RanchModel extends BaseRanchModel
{
    protected $table = 'ranch';

    /**
     * 总条数
     * @param array $params
     * @return int
     */
    public function total(array $params): int
    {
        $query = $this;
        if (!empty($params['keyword'])) {
            $whereRaw = '(real_name like "%' . $params['keyword'] . '%"' .
                        ' or phone like "%' . $params['keyword'] . '%"' .
                        ' or name like "%' . $params['keyword'] . '%")';
            if (is_numeric($params['keyword'])) {
                $whereRaw = trim($whereRaw, ')');
                $whereRaw .= ' or cast(store_ranch.id as char) like "%' . $params['keyword'] . '%")';
            }
            $query = $query->whereRaw($whereRaw);
        }

        return $query->join('store_users', 'store_users.id', '=', 'store_ranch.store_user_id')
                     ->where('store_ranch.deleted', self::DELETED_NO)
                     ->count();
    }

    /**
     * 审核通过的用户的牧场
     * @param array $fields
     * @return array
     */
    public function passList(array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
                    ->join('store_users', 'store_users.id', '=', 'store_ranch.store_user_id')
                    ->where('store_users.status', UserModel::STATUS_DEFAULT)
                    ->where('store_ranch.deleted', self::DELETED_NO)
                    ->orderBy('id', 'desc')
                    ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                    ->limit(PAGE_SIZE)
                    ->get()
                    ->toArray();
    }

    /**
     * 牧场列表
     * @param array $params
     * @param array $fields
     * @return array
     */
    public function listByAdmin(array $params, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $query = $this;
        if (!empty($params['keyword'])) {
            $whereRaw = '(real_name like "%' . $params['keyword'] . '%"' .
                        ' or phone like "%' . $params['keyword'] . '%"' .
                        ' or name like "%' . $params['keyword'] . '%")';
            if (is_numeric($params['keyword'])) {
                $whereRaw = trim($whereRaw, ')');
                $whereRaw .= ' or cast(store_ranch.id as char) like "%' . $params['keyword'] . '%")';
            }
            $query = $query->whereRaw($whereRaw);
        }

        return $query->select($fields)
                     ->join('store_users', 'store_users.id', '=', 'store_ranch.store_user_id')
                     ->where('store_ranch.deleted', self::DELETED_NO)
                     ->orderBy('id', 'desc')
                     ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                     ->limit(PAGE_SIZE)
                     ->get()
                     ->toArray();
    }

    /**
     * 牧场列表
     * @param array $fields
     * @return array
     */
    public function list(array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
                    ->where('store_user_id', MvcHelper::getLoginUserId())
                    ->where('deleted', self::DELETED_NO)
                    ->orderBy('id', 'desc')
                    ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                    ->limit(PAGE_SIZE)
                    ->get()
                    ->toArray();
    }

    /**
     * 根据商户用户ids查询数据
     * @param array $storeUserIds
     * @param array $fields
     * @return array
     */
    public function getItemsByStoreUserIds(array $storeUserIds, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
                    ->whereIn('store_user_id', $storeUserIds)
                    ->where('deleted', self::DELETED_NO)
                    ->orderBy('id', 'desc')
                    ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                    ->limit(PAGE_SIZE)
                    ->get()
                    ->toArray();
    }

    /**
     * 根据商户用户id查询数据
     * @param int $storeUserId
     * @param array $fields
     * @return array
     */
    public function getItemsByStoreUserId(int $storeUserId, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
                       ->where('store_user_id', $storeUserId)
                       ->where('deleted', self::DELETED_NO)
                       ->first();
        return $result ? $result->toArray() : [];
    }
}
